﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Wp.DAL;

namespace Wp.BLL
{
    /// <summary>
    /// Summary description for Per_ActionsRoles
    /// </summary>
    public class TblTermRelationshipsManager : TblTermRelationshipsManagerBase
    {
        public TblTermRelationshipsManager()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public static TblTermRelationshipsManager CreateInstant()
        {
            return new TblTermRelationshipsManager();
        }
        public void Update(DataTable dt)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@iTblTermRelationships", dt));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblTermRelationship", sqlparam);
        }
        public void DeleteByObjectIdAndTaxonomy(int ObjectId, string Taxonomy)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@ObjectId", ObjectId));
            ls.Add(new SqlParameter("@Taxonomy", Taxonomy));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_DeleteTblTermRelationshipByTaxonomy", sqlparam);
        }
        public List<TblTermRelationshipsEntity> SelectTblTermRelationshipAndName(int ObjectId, string Taxonomy)
        {

            List<TblTermRelationshipsEntity> lsresult = new List<TblTermRelationshipsEntity>();
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@ObjectId", ObjectId));
            ls.Add(new SqlParameter("@Taxonomy", Taxonomy));


            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTermRelationshipAndName", sqlparam);

            if (dt != null && dt.Rows.Count > 0)
            {
                TblTermRelationshipsManager item = TblTermRelationshipsManager.CreateInstant();
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(item.GetTblTermRelationshipsEntity(dt.Rows[i]));
            }
            return lsresult;
        }
        public string GetStringTagsList(int ObjectId, string Taxonomy)
        {
            string result = "";
            List<TblTermRelationshipsEntity> ls = TblTermRelationshipsManager.CreateInstant().SelectTblTermRelationshipAndName(ObjectId, Taxonomy);
            if (ls != null && ls.Count > 0)
            {
                for (int i = 0; i < ls.Count; i++)
                {
                    if (i == 0)
                        result = ls[i].Name;
                    else
                        result += "," + ls[i].Name;
                }
            }
            return result;
        }



    }
}
